package cn.tedu.boot21.controller;

import cn.tedu.boot21.entity.User;
import cn.tedu.boot21.mapper.EmpMapper;
import cn.tedu.boot21.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {

    @Autowired
    UserMapper userMapper;

    @RequestMapping("/reg")
    public String reg(User user){
        User u=userMapper.selectByUsername(user.getUsername());
        System.out.println("user = " + user);
        if (u!=null){
            return "用户名已经存在<a href='/index.html'>重新注册</a>";

        }else {
            userMapper.insert(user);
            return "成功!";
        }
    }
    @RequestMapping("/login")
    public String login(User user){
        String name=user.getUsername();
        String userPassword =user.getPassword();
        User u= userMapper.selectByUsername(name);
        if (u!=null){
            if (userPassword.equals(u.getPassword())){
                return  "登录成功<a href='/'>返回首页</a>";
            }
            return "密码错误!<a href='/login.html>重新登录</a>'";
        }else {
            return "用户名不存在<a href='/login.html'>重新登录</a>";
        }
    }

    @RequestMapping("/regA")
    public String regA(@RequestBody User user){
        User u = userMapper.selectByUsername(user.getUsername());
        if (u!=null){
            return "用户名已存在!";
        }
        userMapper.insert(user);
        return "注册成功!";
    }
    @RequestMapping("/loginA")
    public Integer loginA(@RequestBody User user){
        String userName= user.getUsername();
        String userPass= user.getPassword();
        User u=userMapper.selectByUsername(userName);
        if (u!=null){
            if (userPass.equals(u.getPassword())){
                return 1;//登录成功!
            }
            return 3;//密码错误
        }
        return 2;//代表用户名不存在
    }
}
